{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 一、列表的基础操作\n",
    "题目:  \n",
    "1.创建一个包含三个字符串元素的列表 tech_list，元素分别为 “Python”, “Java”, “Go”。  \n",
    "2.获取列表中的第一个元素，并将其存储在变量 first_tech 中。  \n",
    "3.向 tech_list 的末尾添加一个新的字符串元素 “JavaScript”。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Python\n",
      "['Python', 'Java', 'Go', 'JavaScript']\n"
     ]
    }
   ],
   "source": [
    "tech_list = [\"Python\",\"Java\",\"Go\"]  #新建列表\n",
    "first_tech = tech_list[0]\n",
    "print(first_tech)   #取列表第一个元素\n",
    "tech_list += [\"JavaScript\"]\n",
    "print(tech_list)   #给列表末尾新加一个元素\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "第二种方法，append（）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    " %reset -f\n",
    "#强制清除所有变量，如果发现下面的append越加越多，\n",
    "#运行完这条命令，再运行以下上面的代码块，之后再运行下面的代码块就行了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Python', 'Ruby', 'Go', 'JavaScript', '🐱']\n"
     ]
    }
   ],
   "source": [
    "tech_list.append(\"🐱\")\n",
    "#tech_list.append(9)  #还可以加数字\n",
    "print(tech_list)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4.修改 tech_list 中的第二个元素（索引为 1），将其从 “Java” 更改为 “Ruby”。  \n",
    "5.移除列表中的元素 “Go”。  \n",
    "6.计算当前 tech_list 的长度，并将结果存储在变量 current_length 中。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Python', 'Ruby', 'JavaScript']\n",
      "当前列表长度为：3\n",
      "['Python', 'Ruby', 'Go', 'JavaScript']\n"
     ]
    }
   ],
   "source": [
    "tech_list[1] = 'Ruby'  #修改列表中第二个元素\n",
    "tech_list.remove('Go')\n",
    "current_length = len(tech_list)\n",
    "print(f'{tech_list}\\n当前列表长度为：{current_length}')\n",
    "tech_list.insert(-1,'Go')\n",
    "print(tech_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "拓展：在其他位置上新加元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['ovo', 'qwq', 'OmO', 'TvT', 'OUO']\n"
     ]
    }
   ],
   "source": [
    "test = tech_list = [\"ovo\",\"qwq\",\"TvT\",\"OUO\"] \n",
    "test.insert(2,'OmO')\n",
    "print(test)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "拓展：pop方法移除元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Python', 'Java', 'Go']\n"
     ]
    }
   ],
   "source": [
    "tech_list = [\"Python\",\"Go\",\"Java\", \"Go\"]\n",
    "index = tech_list.index(\"Go\")\n",
    "tech_list.pop(index)\n",
    "print(tech_list)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "拓展：使用代码推导式移除元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Python', 'Java']\n"
     ]
    }
   ],
   "source": [
    "tech_list = [\"Python\", \"Java\", \"Go\"]\n",
    "tech_list = [tech for tech in tech_list if tech != \"Go\"]\n",
    "print(tech_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 二、for循环语句"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "题目：  \n",
    "1. 计算1加到100的和 用for循环来写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5050\n"
     ]
    }
   ],
   "source": [
    "sum = 0\n",
    "for i in range(1,101):   #计算1加到100的和 用for循环来写\n",
    "    sum += i\n",
    "print(sum)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2. 判断语句  \n",
    "①定义一个包含整数的列表 scores，赋值为 [85, 92, 78, 65, 95, 88]。  \n",
    "②初始化两个变量：excellent_count 用于记录分数大于等于 90 的个数，初始值为 0；total_score 用于累加所有分数，初始值为 0。  \n",
    "③使用 for 循环遍历 scores 列表中的每一个分数。  \n",
    "④在循环内部：  \n",
    "a.将当前分数累加到 total_score 变量上。  \n",
    "b.使用 if 语句判断当前分数是否大于等于 90。如果是，则将 excellent_count 变量加 1。  \n",
    "⑤循环结束后，计算平均分 average_score（总分除以分数的个数）。\n",
    "⑥使用 f-string 分三行打印出以下信息：  \n",
    "优秀分数（>=90）的个数。  \n",
    "所有分数的总和。  \n",
    "所有分数的平均分（结果包含3位小数）。  \n",
    "打印格式应类似：  \n",
    "优秀分数个数: 3  \n",
    "分数总和: 503  \n",
    "平均分数: 83.833\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "优秀分数个数：2\n",
      "分数总和：503\n",
      "平均分数：83.833\n"
     ]
    }
   ],
   "source": [
    "scores = [85,92,78,65,95,88]\n",
    "excellent_count = 0\n",
    "total_score = 0\n",
    "for i in scores:\n",
    "    total_score += i\n",
    "    if i >= 90:\n",
    "        excellent_count += 1\n",
    "length = len(scores)\n",
    "average_score = total_score / length\n",
    "print(f'优秀分数个数：{excellent_count}')\n",
    "print(f'分数总和：{total_score}')\n",
    "print(f'平均分数：{average_score:.3f}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 三、温度预警系统（if-else）  \n",
    "① 定义一个变量temperature存储当前温度（整数）  \n",
    "② 根据以下条件判断并打印预警信息：  \n",
    "  - 高于35度：打印\"红色预警：高温天气！\"  \n",
    "  - 28-35度：打印\"黄色预警：天气炎热\"  \n",
    "  - 20-27度：打印\"绿色提示：适宜温度\"  \n",
    "  - 低于20度：打印\"蓝色预警：注意保暖\"  \n",
    "③ 使用if-elif-else结构实现  \n",
    "④ 测试用例：用38你的代码\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "热死啦！\n"
     ]
    }
   ],
   "source": [
    "input_temp = input(\"输入一个温度\")\n",
    "temperature = int(input_temp)\n",
    "if temperature > 35:\n",
    "    print('热死啦！')\n",
    "elif temperature > 28:\n",
    "    print('天气炎热')\n",
    "elif temperature > 20:\n",
    "    print('适宜温度')\n",
    "else:\n",
    "    print('注意保暖，别冻着')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "拓展：输入多行文本"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入多行文本，输入 'end' 结束输入:\n",
      "你输入的文本是:\n",
      "今天在写毕业论文\n",
      "突然饿了\n",
      "吃了个馕\n"
     ]
    }
   ],
   "source": [
    "lines = []\n",
    "print(\"请输入多行文本，输入 'end' 结束输入:\")\n",
    "while True:\n",
    "    line = input()\n",
    "    if line == 'end':\n",
    "        break\n",
    "    lines.append(line)\n",
    "\n",
    "print(\"你输入的文本是:\")\n",
    "for line in lines:\n",
    "    print(line)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "pylearn",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.21"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
